//
//  LatestOrderViewController.m
//  beautyLamp
//
//  Created by Tam Kwok To Michael on 2017/8/25.
//  Copyright © 2017年 Tam Kwok To Michael. All rights reserved.
//

#import "LatestOrderViewController.h"
#import "OrderTableViewCell.h"
#import "BLHeader.h"
#import "BLOrder.h"
#import "Product.h"
#import "OrderRefuseViewController.h"
#import "MyOrderNet.h"
#import <MJRefresh.h>
#import "CustomerOrderDetailViewController.h"

@interface LatestOrderViewController () <UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSArray *orders;
@property (nonatomic, assign) NSInteger pageIndex;

@end

@implementation LatestOrderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}

- (void)getData {
    if (self.tableView.mj_header.isRefreshing) {
        self.pageIndex = 0;
    }
   [MyOrderNet getOrderList:self.state index:self.pageIndex completer:^(id resultObject, NSError *error) {
       if (resultObject) {
           self.orders = resultObject;
       }
       if ([resultObject count] == 0) {
           self.tableView.mj_footer.hidden = YES;
       }else {
           self.tableView.mj_footer.hidden = NO;
       }
       
       [self.tableView.mj_header endRefreshing];
       self.tableView.mj_footer.state = MJRefreshStateIdle;
       [self.tableView reloadData];
   }];
}

- (void)getMore {
    self.pageIndex = _pageIndex + 15;
    [MyOrderNet getOrderList:self.state index:self.pageIndex completer:^(id resultObject, NSError *error) {
        if (resultObject) {
            NSMutableArray *array = [NSMutableArray  arrayWithArray:self.orders];
            [array addObject:resultObject];
        }
        [self.tableView.mj_footer endRefreshing];
        if ([resultObject count] < 15) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
        [self.tableView reloadData];
    }];
    
}

- (void)setupUI {
    self.pageIndex = 0;
    [self.tableView registerNib:[UINib nibWithNibName:@"OrderTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:ReuseIdentify];
     MJRefreshHeader *refreshHead = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(getData)];
    self.tableView.mj_header = refreshHead;
    [self.tableView.mj_header beginRefreshing];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(getMore)];
    self.tableView.mj_footer.hidden = YES;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notifyUpdateList) name:@"updateOrderList" object:nil];
}

- (void)notifyUpdateList {
    [self getData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.orders count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    BLOrder *order = _orders[indexPath.row];
    OrderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ReuseIdentify];
    [cell setupUI:order];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 121.5;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    BLOrder *order = _orders[indexPath.row];
    NSString *userClass = [[NSUserDefaults standardUserDefaults] objectForKey:USERCLASS];
    if ([order.orderState integerValue]== -1) {
        OrderRefuseViewController *controller = [[OrderRefuseViewController alloc] init];
        controller.order = order;
        [self.navigationController pushViewController:controller animated:YES];
    }else {
        CustomerOrderDetailViewController *customer = [[CustomerOrderDetailViewController alloc] init];
        customer.order = order;
        [self.navigationController pushViewController:customer animated:YES];
    }
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.tableView reloadData];
}
@end
